﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.dao.impl;

import java.util.*;

import com.hh.bss.auth.util.AuthSqlUtil;
import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.util.*;





import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

@Repository
public class CustomerComplainDaoImpl extends BaseHibernateDao<CustomerComplain,java.lang.Integer>  implements CustomerComplainDao<CustomerComplain,java.lang.Integer>{

	public Class getEntityClass() {
		return CustomerComplain.class;
	}
	
	public Page findPage(CustomerComplainQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
        
		
		    Class[] os={CustomerComplainQuery.class};
    		Class property= org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
        if (property!=null){
        	 if ("java.lang.String".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(),query.getQuery());
	    			
	    		}
	    		else if ("java.lang.Integer".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));
	    			
	    		}
	    		else if ("java.lang.Double".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
	    		}
	    		else if ("java.util.Date".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
	    		}
        }	 
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append(" select * from ( ");
		sql.append(" select a.id,a.cust_id custId,a.complain_dt complainDt,a.complainer,b.cv_name complainerName,a.be_complainer beComplainer,a.handler handler1,a.creator,a.create_dt createDt,a.complain_detail complainDetail,a.handle_record handleRecord,a.handle_status handleStatus,a.modifier,a.modified_dt modifiedDt from customer_complain a ");
		sql.append(" left join cv b on a.complainer=b.id ");
		sql.append(" ) t where 1=1 ");
        
		if(isNotEmpty(query.getCustId())) {
            sql.append(" and  t.custId = ? ");
            values.add(query.getCustId());
            
        }
		if (query.getOnlineUser()!=null){
  			String authsql=AuthSqlUtil.authCustomerProtectSQL(query.getOnlineUser().getUsername(),"t.custId", "t.creator");
  			if (!StringHelper.isEmpty(authsql)) sql.append(authsql);			
  	    }
		return super.getJdbcDao().createPage(sql.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
	}
	

}
